<?php

namespace App\HttpController\Api;

use App\Lib\ClassArr;

class Upload extends Base
{

    public function file()
    {
        $request = $this->request();
        $files = $request->getSwooleRequest()->files;
        $types = array_keys($files);
        $type = $types[0];
        try{
            // 判断类型
            // if($type === 'video'){
            //     $class = '\App\Lib\Upload\Video';
            // }elseif($type === 'image'){
            //     $class = '\App\Lib\Upload\Image';
            // }
            // $obj  = new $class($request);
            // $file = $obj->upload();
            $classObj = new ClassArr();
            $claasStats = $classObj->uploadClassStat();
            $uploadObj = $classObj->initClass($type,$claasStats,[$request],true);
            $file = $uploadObj->upload(1);
        }catch(\Exception $e){
            return $this->writeJson(400,$e->getMessage(),[]);
        }
        if(empty($file)){
            return $this->writeJson(400,"上传失败",[]);
        }
        $data = [
            'url' => $file,
        ];
        return $this->writeJson(200,'OK',$data);
        // $videos = $request->getUploadedFile('file');
        // $flag = $videos->moveTo('/home/wwwroot/easyswoole/1.mp4');
        // $data = [
        //     'url' => '1.mp4',
        //     'flag' => $flag,
        // ];
        // if($flag){
        //     return $this->writeJson(200,'OK',$data);
        // }else{
        //     return $this->writeJson(400,'error',[]);
        // }
    }

}